{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Copyright &copy; 2012 Ondrej Martinsky, All rights reserved\n",
    "\n",
    "[www.quantandfinancial.com](http://www.quantandfinancial.com)\n",
    "# Time Value of Money"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Implementation\n",
    "scroll below for examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from optimization import newton\n",
    "class TVM:\n",
    "    bgn, end = 0, 1\n",
    "\n",
    "    def __str__(self):\n",
    "        return \"n=%f, r=%f, pv=%f, pmt=%f, fv=%f\" % (\n",
    "            self.n, self.r, self.pv, self.pmt, self.fv)\n",
    "\n",
    "    def __init__(self, n=0.0, r=0.0, pv=0.0, pmt=0.0, fv=0.0, mode=end):\n",
    "        self.n = float(n)\n",
    "        self.r = float(r)\n",
    "        self.pv = float(pv)\n",
    "        self.pmt = float(pmt)\n",
    "        self.fv = float(fv)\n",
    "        self.mode = mode\n",
    "\n",
    "    def calc_pv(self):\n",
    "        z = pow(1 + self.r, -self.n)\n",
    "        pva = self.pmt / self.r\n",
    "        if (self.mode == TVM.bgn): pva += self.pmt\n",
    "        return -(self.fv * z + (1 - z) * pva)\n",
    "\n",
    "    def calc_fv(self):\n",
    "        z = pow(1 + self.r, -self.n)\n",
    "        pva = self.pmt / self.r\n",
    "        if (self.mode == TVM.bgn): pva += self.pmt\n",
    "        return -(self.pv + (1 - z) * pva) / z\n",
    "\n",
    "    def calc_pmt(self):\n",
    "        z = pow(1 + self.r, -self.n)\n",
    "        if self.mode == TVM.bgn:\n",
    "            return (self.pv + self.fv * z) * self.r / (z - 1) / (1 + self.r)\n",
    "        else:\n",
    "            return (self.pv + self.fv * z) * self.r / (z - 1)\n",
    "\n",
    "    def calc_n(self):\n",
    "        pva = self.pmt / self.r\n",
    "        if (self.mode == TVM.bgn): pva += self.pmt\n",
    "        z = (-pva - self.pv) / (self.fv - pva)\n",
    "        return -log(z) / log(1 + self.r)\n",
    "\n",
    "    def calc_r(self):\n",
    "        def function_fv(r, self):\n",
    "            z = pow(1 + r, -self.n)\n",
    "            pva = self.pmt / r\n",
    "            if (self.mode == TVM.bgn): pva += self.pmt\n",
    "            return -(self.pv + (1 - z) * pva) / z\n",
    "        return newton(f=function_fv, fArg=self, x0=.05,\n",
    "                      y=self.fv, maxIter=1000, minError=0.0001)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Calculation of monthly payments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-2639.1842014888507"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "TVM(n=25*12, r=.04/12, pv=500000, fv=0).calc_pmt()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Internal Rate of Return"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.045432970406767303"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "TVM(n=10*2, pmt=6/2, pv=-80, fv=100).calc_r()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How much I can borrow ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "378904.96594965487"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "TVM(n=25*12, r=.04/12, pmt=-2000, fv=0).calc_pv()"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
